|
|
|
|
|
|
|
The code in Listing 28.3 adds a new record to the Authors table in the BIBLIO database. |
|
|
|
|
|
|
|
|
Listing 28.3 ADOTEST.ZIPAdding a Record to an ADO Recordset |
|
|
|
|
|
|
|
|
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
cn.Open "DSN=BIBLIO"
'OPEN A RECORDSET
Set rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Source = "Authors"
rs.ActiveConnection = cn
rs.Open
'ADD A NEW RECORD
rs.AddNew
rs.Fields("Author") = "King, Stevie"
rs.Fields("Year Born") = 1945
rs.Update
'NOTE: THE Au_ID field is an autonumber field,
' it will be created automatically.
'DESTROY THE OBJECTS
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing |
|
|
|
|
|
|
|
|
When you are working with a set of data, most of the time you are working with properties and methods of the Recordset object. However, to retrieve that data, you will find that the ADO Command object is indispensable. It allows you to encapsulate a query or SQL stored procedure into a reusable object, which is especially ideal if you need to perform the operation multiple times. You can even store the parameters from your query or stored procedure in the object's Parameters collection, which means you don't have to worry about building an appropriate SQL string with code. After you set up a command object, you can change the parameters of the object and call it repeatedly. |
|
|
|
|
|
|
|
|
For example, recall the SQL statement from Listing 28.1, which was used to select authors whose last names begin with the letter Q. Using Microsoft Access, you can create a generic query called LetterLookup by using the following SQL syntax: |
|
|
|
|
|
|
|
|
PARAMETERS Letter Text;
SELECT *
FROM Authors
WHERE Author Like Letter+'*'; |
|
|
|
|
|